package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.QuestionsAnswers;
import org.tea.inventory.db.service.QuestionsAnswersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/questions")
public class QuestionsAnwsersController {

    @Autowired
    private QuestionsAnswersService questionsAnswersService;

    /**
     * 获取banner信息列表
     */
    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId, String questions, Boolean isAnswer,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       String sort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        QuestionsAnswers questionsAnswers = new QuestionsAnswers();
        if(questions != null)
        	questionsAnswers.setQuestions(questions);
    	if(isAnswer != null)
        	questionsAnswers.setIsAnswer(isAnswer);
        List<QuestionsAnswers> lstQuestionsAnswers = questionsAnswersService.querySelective(questionsAnswers, page, limit, sort);

        int total = questionsAnswersService.countSelective(questionsAnswers);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", lstQuestionsAnswers);
        data.put("total", total);

        return ResponseUtil.ok(data);	
		
    }

    /**
     * 获取指定id的banner详情
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return  ResponseUtil.badArgumentValue();
        }

        QuestionsAnswers questionsAnswers = questionsAnswersService.findById(id);

        return ResponseUtil.ok(questionsAnswers);
    }

    /**
     * 创建问题
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody QuestionsAnswers questionsAnswers) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        questionsAnswers.setCreatorId(adminId);
        questionsAnswers.setCreateDate(new Date());
        questionsAnswersService.add(questionsAnswers);

        return ResponseUtil.ok(questionsAnswers);
    }

    /**
     * 更新问题
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody QuestionsAnswers questionsAnswers) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        questionsAnswersService.update(questionsAnswers);

        return ResponseUtil.ok(questionsAnswers);
    }

    /**
     * 删除问题
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody QuestionsAnswers questionsAnswers) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(questionsAnswers == null) {
            return ResponseUtil.badArgumentValue();
        }

        questionsAnswersService.delete(questionsAnswers.getId());

        return ResponseUtil.ok();
    }
}
